﻿using System.Web.Mvc;
using BusinessBingo.Model;
using BusinessBingo.Model.Interfaces;
using BusinessBingo.Web.Controllers.Base;
using BusinessBingo.Web.Infrastructure.InfoStores.Interfaces;
using BusinessBingo.Web.Models.ViewModels;

namespace BusinessBingo.Web.Controllers
{
    /// <summary>
    /// Controller for "small" stuff.
    /// </summary>
    public class MetaController : SessionlessBaseController
    {
        /// <summary>
        /// The language store is responsible for setting a specific language.
        /// </summary>
        private ICurrentLanguageStore _languageStore;

        /// <summary>
        /// Ctor for new instances
        /// </summary>
        /// <param name="currentLanguageStore"></param>
        public MetaController(ICurrentLanguageStore currentLanguageStore)
            : base()
        {
            _languageStore = currentLanguageStore;
        }

        /// <summary>
        /// Switch the UI language to a specific language.
        /// </summary>
        /// <param name="key">The specific language key.</param>
        /// <returns>Redirects to the start page.</returns>
        public RedirectToRouteResult SwitchLanguage(LanguageKey key)
        {
            this._languageStore.SetPreferredLanguage(key);
            return RedirectToAction("Index", "Home");
        }

        public ActionResult LanguageBox()
        {
            LanguageKey languageNow = this._languageStore.GetPreferredLanguage();
            LanguageBoxViewModel model = new LanguageBoxViewModel();

            if (languageNow == LanguageKey.De) model.AvailableLanguage = LanguageKey.En;
            else model.AvailableLanguage = LanguageKey.De;

            return View(model);
        }

        /// <summary>
        /// Some scripts should not be rendered if we are in development mode (e.g. analytics) . To check if we are in "dev-mode", we check the host value of the request. 
        /// </summary>
        /// <param name="preventFromRenderingInDevelopment">If host == localhost and this value is true - emptyresult</param>
        /// <param name="name">name of the script</param>
        /// <returns>EmptyResult or ViewResult</returns>
        public ActionResult Render3rdPartyScripts(bool preventFromRenderingInDevelopment, string name)
        {
            string prefix = "3rdPartyScripts_";

            if (preventFromRenderingInDevelopment)
            {
                if (this.Request.Url.Host == "localhost")
                {
                    return new EmptyResult();
                }
            }

            string script = prefix + name;
            return View(script);
        }
    }
}
